from django.db import models
from datetime import datetime
from login.models import MyUser


# Create your models here.
class Chapter(models.Model):
    name = models.CharField(max_length=200)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'章节'
        verbose_name_plural = u'章节'

    def __str__(self):
        return self.name


class Section(models.Model):
    name = models.CharField(max_length=200)
    url = models.CharField(max_length=200)
    file_name = models.CharField(max_length=200, default='')
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)
    chapter = models.ForeignKey(Chapter, related_name='sections', on_delete=models.CASCADE, default=1)

    class Meta:
        verbose_name = u'小节'
        verbose_name_plural = u'小节'

    def __str__(self):
        return self.name


class MyModel(models.Model):
    name = models.CharField(max_length=200)
    classname = models.CharField(max_length=200)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'模型'
        verbose_name_plural = u'模型'

    def __str__(self):
        return self.name


class SectionAction(models.Model):
    name = models.CharField(max_length=200)
    # 0默认 1视频 2动图 3模型
    mytype = models.IntegerField(default=0)
    url = models.CharField(max_length=200)
    page = models.IntegerField(default=0)
    model = models.ForeignKey(MyModel, related_name='actions', on_delete=models.CASCADE, default=1)
    section = models.ForeignKey(Section, related_name='actions', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'课件附件'
        verbose_name_plural = u'课件附件'

    def __str__(self):
        return self.name


class FileBackUp(models.Model):
    name = models.CharField(max_length=200)
    # 0默认 1视频 2动图 3课件
    mytype = models.IntegerField(default=0)
    url = models.CharField(max_length=200)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'文件备份列表'
        verbose_name_plural = u'文件备份列表'

    def __str__(self):
        return self.name


class Example(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    section = models.ForeignKey(Section, related_name='examples', on_delete=models.CASCADE, default=1)
    mymodel = models.ForeignKey(MyModel, related_name='examples', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'算例'
        verbose_name_plural = u'算例'

    def __str__(self):
        return self.name


class ExampleParamPicture(models.Model):
    url = models.CharField(max_length=200)
    file_name = models.CharField(max_length=200)
    example = models.ForeignKey(Example, related_name='param_pictures', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'算例参数图片'
        verbose_name_plural = u'算例参数图片'

    def __str__(self):
        return self.example.name + "_" + self.file_name


class ExampleFuncPicture(models.Model):
    url = models.CharField(max_length=200)
    file_name = models.CharField(max_length=200)
    example = models.ForeignKey(Example, related_name='func_pictures', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'算例函数图片'
        verbose_name_plural = u'算例函数图片'

    def __str__(self):
        return self.example.name + "_" + self.file_name


class ExampleParam(models.Model):
    name = models.CharField(max_length=200)
    pagename = models.CharField(max_length=200, default='')
    default_value = models.FloatField(default=0)
    example = models.ForeignKey(Example, related_name='params', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'算例参数'
        verbose_name_plural = u'算例参数'

    def __str__(self):
        return self.example.name + "_" + self.name


class Note(models.Model):
    content = models.CharField(max_length=200)
    is_public = models.IntegerField(default=1)
    section = models.ForeignKey(Section, related_name='notes', on_delete=models.CASCADE, default=1)
    user = models.ForeignKey(MyUser, related_name='notes', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'笔记'
        verbose_name_plural = u'笔记'

    def __str__(self):
        return self.user.username + '_' + self.section.name


class NotePicture(models.Model):
    url = models.CharField(max_length=200)
    file_name = models.CharField(max_length=200)
    note = models.ForeignKey(Note, related_name='pictures', on_delete=models.CASCADE, default=1)

    class Meta:
        verbose_name = u'笔记图片'
        verbose_name_plural = u'笔记图片'

    def __str__(self):
        return self.file_name


class Comment(models.Model):
    content = models.CharField(max_length=200)
    note = models.ForeignKey(Note, related_name='comments', on_delete=models.CASCADE, default=1)
    user = models.ForeignKey(MyUser, related_name='comments', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'评论'
        verbose_name_plural = u'评论'

    def __str__(self):
        return self.user.username + '_笔记' + str(self.note.id) + '评论'


class CommentPicture(models.Model):
    url = models.CharField(max_length=200)
    file_name = models.CharField(max_length=200)
    comment = models.ForeignKey(Comment, related_name='pictures', on_delete=models.CASCADE, default=1)

    class Meta:
        verbose_name = u'评论图片'
        verbose_name_plural = u'评论图片'

    def __str__(self):
        return self.file_name


class NoteLikes(models.Model):
    number = models.IntegerField(default=0)
    note = models.ForeignKey(Note, related_name='likes', on_delete=models.CASCADE, default=1)
    user = models.ForeignKey(MyUser, related_name='likes', on_delete=models.CASCADE, default=1)
    CreatedAt = models.DateTimeField(max_length=200, auto_now_add=True)
    UpdatedAt = models.DateTimeField(max_length=200, auto_now=True)

    class Meta:
        verbose_name = u'笔记点赞'
        verbose_name_plural = u'笔记点赞'

    def __str__(self):
        return self.user.username + '_笔记' + str(self.note.id) + '点赞'

